package com.moly.grpc.client.studentmessage;

import com.moly.grpc.routeguide.RouteGuideGrpc;
import com.moly.grpc.studentmessage.Result;
import com.moly.grpc.studentmessage.Student;
import com.moly.grpc.studentmessage.StudentServiceGrpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.stub.StreamObserver;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class StudentMessageClient {


    private static StudentServiceGrpc.StudentServiceBlockingStub studentServiceBlockingStub = null;

    private static StudentServiceGrpc.StudentServiceStub studentServiceStub = null;

    private static ManagedChannel channel = null;

    public static void main(String[] args) {

        channel = ManagedChannelBuilder.forAddress("localhost", 8999).usePlaintext().build();

//        getStreamResult();

//        getResultByStreamRequest();

        getStreamResultByStreamRequest();

        try {
            channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }

    private static void testRpc1() {
        Student student = Student.newBuilder().setAge(11).setName("夏末").setHight(179.2f).build();
        Result result = studentServiceBlockingStub.getResult(student);
        System.out.println(result.toString());

    }

    private static void getStreamResult() {

        studentServiceBlockingStub = StudentServiceGrpc.newBlockingStub(channel);
        Student student = Student.newBuilder().setAge(11).setName("夏末").setHight(179.2f).build();
        Iterator<Result> streamResult = studentServiceBlockingStub.getStreamResult(student);
        while (streamResult.hasNext()) {
            Result next = streamResult.next();
            System.out.println(next.toString());

            System.out.println(LocalDateTime.now());
        }
        System.out.println("send  over ");


    }


    private static void getResultByStreamRequest() {

        studentServiceStub = StudentServiceGrpc.newStub(channel);

        StreamObserver<Student> resultByStreamRequest = studentServiceStub.getResultByStreamRequest(new StreamObserver<Result>() {
            @Override
            public void onNext(Result value) {

                System.out.println(value.toString());
                System.out.println("+++");

            }

            @Override
            public void onError(Throwable t) {

            }

            @Override
            public void onCompleted() {

            }
        });

        Set<Student> students = new HashSet<>();
        students.add(createRandomStudent());
        students.add(createRandomStudent());
        students.add(createRandomStudent());
        students.add(createRandomStudent());
        students.add(createRandomStudent());



        students.forEach((s) -> {
            resultByStreamRequest.onNext(s);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        resultByStreamRequest.onCompleted();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }


    private  static  void getStreamResultByStreamRequest(){


        studentServiceStub = StudentServiceGrpc.newStub(channel);

        StreamObserver<Student> streamResultByStreamRequest =
                studentServiceStub.getStreamResultByStreamRequest(new StreamObserver<Result>() {
            @Override
            public void onNext(Result value) {

                System.out.println(LocalDateTime.now()+"  服务器发送  "+value.toString());
            }

            @Override
            public void onError(Throwable t) {

            }

            @Override
            public void onCompleted() {

            }
        });

        Set<Student> students = new HashSet<>();
        students.add(createRandomStudent());
        students.add(createRandomStudent());
        students.add(createRandomStudent());
        students.add(createRandomStudent());
        students.add(createRandomStudent());

        students.forEach(student ->{
            streamResultByStreamRequest.onNext(student);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } );

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        streamResultByStreamRequest.onCompleted();
    }



    private static Student createRandomStudent() {

        return Student.newBuilder().setAge(new Random().nextInt()).setName("夏末" + new Random().nextFloat()).setHight(new Random().nextFloat()).build();
    }
}
